home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / lpc / RCS / lpc.c,v < prev    next >
Encoding:
Text File  |  1992-06-11  |  5.0 KB  |  305 lines

  1. head     1.3;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.3
  10. date     92.06.10.17.24.12;  author jhh;  state Exp;
  11. branches ;
  12. next     1.2;
  13.  
  14. 1.2
  15. date     89.01.11.08.53.05;  author rab;  state Exp;
  16. branches ;
  17. next     1.1;
  18.  
  19. 1.1
  20. date     88.11.23.10.34.54;  author rab;  state Exp;
  21. branches ;
  22. next     ;
  23.  
  24.  
  25. desc
  26. @@
  27.  
  28.  
  29. 1.3
  30. log
  31. @added definition of "debug" so common.c would compile
  32. @
  33. text
  34. @/*
  35.  * Copyright (c) 1983 Regents of the University of California.
  36.  * All rights reserved.
  37.  *
  38.  * Redistribution and use in source and binary forms are permitted
  39.  * provided that this notice is preserved and that due credit is given
  40.  * to the University of California at Berkeley. The name of the University
  41.  * may not be used to endorse or promote products derived from this
  42.  * software without specific prior written permission. This software
  43.  * is provided ``as is'' without express or implied warranty.
  44.  */
  45.  
  46. #ifndef lint
  47. char copyright[] =
  48. "@@(#) Copyright (c) 1983 Regents of the University of California.\n\
  49.  All rights reserved.\n";
  50. #endif /* not lint */
  51.  
  52. #ifndef lint
  53. static char sccsid[] = "@@(#)lpc.c    5.5 (Berkeley) 5/5/88";
  54. #endif /* not lint */
  55.  
  56. /*
  57.  * lpc -- line printer control program
  58.  */
  59. #include <stdio.h>
  60. #include <signal.h>
  61. #include <ctype.h>
  62. #include <setjmp.h>
  63. #include <syslog.h>
  64.  
  65. #include "lpc.h"
  66.  
  67. int    fromatty;
  68.  
  69. char    cmdline[200];
  70. int    margc;
  71. char    *margv[20];
  72. int    top;
  73. int    intr();
  74. struct    cmd *getcmd();
  75. extern int NCMDS;
  76.  
  77. int debug = 0;
  78. jmp_buf    toplevel;
  79.  
  80. main(argc, argv)
  81.     char *argv[];
  82. {
  83.     register struct cmd *c;
  84.     extern char *name;
  85.  
  86.     name = argv[0];
  87.     openlog("lpd", 0, LOG_LPR);
  88.  
  89.     if (--argc > 0) {
  90.         c = getcmd(*++argv);
  91.         if (c == (struct cmd *)-1) {
  92.             printf("?Ambiguous command\n");
  93.             exit(1);
  94.         }
  95.         if (c == 0) {
  96.             printf("?Invalid command\n");
  97.             exit(1);
  98.         }
  99.         if (c->c_priv && getuid()) {
  100.             printf("?Privileged command\n");
  101.             exit(1);
  102.         }
  103.         (*c->c_handler)(argc, argv);
  104.         exit(0);
  105.     }
  106.     fromatty = isatty(fileno(stdin));
  107.     top = setjmp(toplevel) == 0;
  108.     if (top)
  109.         signal(SIGINT, intr);
  110.     for (;;) {
  111.         cmdscanner(top);
  112.         top = 1;
  113.     }
  114. }
  115.  
  116. intr()
  117. {
  118.     if (!fromatty)
  119.         exit(0);
  120.     longjmp(toplevel, 1);
  121. }
  122.  
  123. /*
  124.  * Command parser.
  125.  */
  126. cmdscanner(top)
  127.     int top;
  128. {
  129.     register struct cmd *c;
  130.  
  131.     if (!top)
  132.         putchar('\n');
  133.     for (;;) {
  134.         if (fromatty) {
  135.             printf("lpc> ");
  136.             fflush(stdout);
  137.         }
  138.         if (gets(cmdline) == 0)
  139.             quit();
  140.         if (cmdline[0] == 0)
  141.             break;
  142.         makeargv();
  143.         c = getcmd(margv[0]);
  144.         if (c == (struct cmd *)-1) {
  145.             printf("?Ambiguous command\n");
  146.             continue;
  147.         }
  148.         if (c == 0) {
  149.             printf("?Invalid command\n");
  150.             continue;
  151.         }
  152.         if (c->c_priv && getuid()) {
  153.             printf("?Privileged command\n");
  154.             continue;
  155.         }
  156.         (*c->c_handler)(margc, margv);
  157.     }
  158.     longjmp(toplevel, 0);
  159. }
  160.  
  161. extern struct cmd cmdtab[];
  162.  
  163. struct cmd *
  164. getcmd(name)
  165.     register char *name;
  166. {
  167.     register char *p, *q;
  168.     register struct cmd *c, *found;
  169.     register int nmatches, longest;
  170.     int ncmds;
  171.  
  172.     longest = 0;
  173.     nmatches = 0;
  174.     found = 0;
  175.     for (c = cmdtab, ncmds = NCMDS; --ncmds >= 0; c++) {
  176.             p = c->c_name;
  177.         for (q = name; *q == *p++; q++)
  178.             if (*q == 0)        /* exact match? */
  179.                 return(c);
  180.         if (!*q) {            /* the name was a prefix */
  181.             if (q - name > longest) {
  182.                 longest = q - name;
  183.                 nmatches = 1;
  184.                 found = c;
  185.             } else if (q - name == longest)
  186.                 nmatches++;
  187.         }
  188.     }
  189.     if (nmatches > 1)
  190.         return((struct cmd *)-1);
  191.     return(found);
  192. }
  193.  
  194. /*
  195.  * Slice a string up into argc/argv.
  196.  */
  197. makeargv()
  198. {
  199.     register char *cp;
  200.     register char **argp = margv;
  201.  
  202.     margc = 0;
  203.     for (cp = cmdline; *cp;) {
  204.         while (isspace(*cp))
  205.             cp++;
  206.         if (*cp == '\0')
  207.             break;
  208.         *argp++ = cp;
  209.         margc += 1;
  210.         while (*cp != '\0' && !isspace(*cp))
  211.             cp++;
  212.         if (*cp == '\0')
  213.             break;
  214.         *cp++ = '\0';
  215.     }
  216.     *argp++ = 0;
  217. }
  218.  
  219. #define HELPINDENT (sizeof ("directory"))
  220.  
  221. /*
  222.  * Help command.
  223.  */
  224. help(argc, argv)
  225.     int argc;
  226.     char *argv[];
  227. {
  228.     register struct cmd *c;
  229.  
  230.     if (argc == 1) {
  231.         register int i, j, w;
  232.         int columns, width = 0, lines;
  233.  
  234.         printf("Commands may be abbreviated.  Commands are:\n\n");
  235.         for (c = cmdtab; c < &cmdtab[NCMDS]; c++) {
  236.             int len = strlen(c->c_name);
  237.  
  238.             if (len > width)
  239.                 width = len;
  240.         }
  241.         width = (width + 8) &~ 7;
  242.         columns = 80 / width;
  243.         if (columns == 0)
  244.             columns = 1;
  245. #ifdef sprite
  246.         lines = (NCMDS + columns) / columns;
  247. #else
  248.         lines = (NCMDS + columns - 1) / columns;
  249. #endif
  250.         for (i = 0; i < lines; i++) {
  251.             for (j = 0; j < columns; j++) {
  252.                 c = cmdtab + j * lines + i;
  253.                 printf("%s", c->c_name);
  254.                 if (c + lines >= &cmdtab[NCMDS]) {
  255.                     printf("\n");
  256.                     break;
  257.                 }
  258.                 w = strlen(c->c_name);
  259.                 while (w < width) {
  260.                     w = (w + 8) &~ 7;
  261.                     putchar('\t');
  262.                 }
  263.             }
  264.         }
  265.         return;
  266.     }
  267.     while (--argc > 0) {
  268.         register char *arg;
  269.         arg = *++argv;
  270.         c = getcmd(arg);
  271.         if (c == (struct cmd *)-1)
  272.             printf("?Ambiguous help command %s\n", arg);
  273.         else if (c == (struct cmd *)0)
  274.             printf("?Invalid help command %s\n", arg);
  275.         else
  276.             printf("%-*s\t%s\n", HELPINDENT,
  277.                 c->c_name, c->c_help);
  278.     }
  279. }
  280. @
  281.  
  282.  
  283. 1.2
  284. log
  285. @*** empty log message ***
  286. @
  287. text
  288. @d44 1
  289. @
  290.  
  291.  
  292. 1.1
  293. log
  294. @Initial revision
  295. @
  296. text
  297. @d42 1
  298. d136 1
  299. d141 2
  300. a142 1
  301.     for (c = cmdtab; p = c->c_name; c++) {
  302. a198 1
  303.         extern int NCMDS;
  304. @
  305.